var gulp = require('gulp');
//Utility functions for gulp plugins
var gutil = require('gulp-util');
var webpack = require('webpack');
var gulpWebpack = require('webpack-stream');
//when some errors take place in the process of gulp's task, plumber will
//ignore these errors, this is very convenient for gulp's watch,because when
//some errors take place in the process of gulp's watch, this watch will terminate.
//plumber will make this watch continue to monitor.
var plumber = require('gulp-plumber');
var compass = require('gulp-compass');
var path = require('path');

var paths = {
    webpackMonitorPath: ['assets/**/*.*', 'public/template/**/*.*', '!assets/**/*.scss'],
    devDependence: ['node_modules/ng2-bootstrap/ng2-bootstrap.js'],
    compassMonitorPath: ['assets/**/*.scss']
};

gulp.task('dev', function () {
    gulp.src(paths.devDependence)
        .pipe(gulp.dest('public/dist/'));
});

gulp.task('watch', function () {
    gulp.watch(paths.webpackMonitorPath, ['webpack']);
    // gulp.watch(paths.compassMonitorPath, ['runCompassAndWebpack']);
});

//the compass will run first before webpack task runs.
gulp.task('runCompassAndWebpack', ['compass', 'webpack']);

gulp.task('compass', function () {
    var flag = false;
    var result = gulp.src(paths.compassMonitorPath)
        .pipe(plumber())
        .pipe(compass({
            config_file: 'config.rb',
            css: 'assets/styleDist',
            sass: 'assets/styleSource'
        }));
});

gulp.task('webpack', function () {
    return gulp.src('assets/*.ts')
        .pipe(plumber())
        .pipe(gulpWebpack(require('./webpack.config.js'), webpack))
        .pipe(gulp.dest("."));
});